﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.MobileControls;
using System.Web.UI.WebControls;
using DLV.Data.Entities;
using DLV.FrameWork;
using DLV.Utilities;
using DLV.Library.Utilities;
using Label = System.Web.UI.WebControls.Label;

namespace DLV.BackEnd.Pages
{
    public partial class CusRequest : AdminPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadGroupType();
                LoadTourType();
                LoadMartName();
                LoadSeason();
                LoadGv(0);
                LoadNational();
                LoadGvKhachHang(0);

            }

        }
        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Label txtMa = (Label)(GridView2.Rows[e.NewSelectedIndex].FindControl("lbMa"));
            new CustomersController().DeleteCustomers(Convert.ToInt32(txtMa.Text));
        }



        #region khách hàng
        public void LoadGvKhachHang(int indexPage)
        {
            CustomersInfo info = new CustomersInfo();
            info.CusFullName = txtCusFullName.Text;
            info.CusCode = txtCusCode.Text;
            info.CusPhone = txtCusPhone.Text;
            info.CusEmail = txtCusEmail.Text;
            info.CusAddress = txtCusAddress.Text;
            info.CusGender = Convert.ToInt32(RadioButtonList1.SelectedValue);
            info.CusNationalId = Convert.ToInt32(ddlCusNationalId.SelectedValue);
            info.CusGender = Convert.ToInt32(RadioButtonList1.SelectedValue);
            info.CusNotes = txtCusNotes.Text;
            info.CusStatus = 0;
            List<CustomersInfo> list = new CustomersController().ListCustomersSearch(info);
            DataTable tb = CBO.ConvertToDataTable(list, typeof(CustomersInfo));
            GvPhanTrang(tb, ref GridView2, 20, indexPage);
        }
        private void LoadTourType()
        {
            ddlTourType.DataSource = new TourTypeController().ListTourType().Where(p => p.Status == (int)Globals.DocumentStatus.Use);
            ddlTourType.DataTextField = "TourTypeName";
            ddlTourType.DataValueField = "TourTypeId";
            ddlTourType.DataBind();

            ddlTourType.Items.Insert(0, new ListItem("--- Chọn loại tour ---", "-1"));
        }
        private void LoadMartName()
        {
            ddlMartName.Items.Insert(0, new ListItem("--- Chọn thị trường ---", "-1"));
            ddlMartName.Items.Insert(1, new ListItem("Inbound", "Inbound"));
            ddlMartName.Items.Insert(2, new ListItem("Outbound", "Outbound"));
            ddlMartName.Items.Insert(3, new ListItem("Nội địa", "Nội địa"));
        }
        private void LoadSeason()
        {
            ddlSeason.Items.Add("Thấp điểm");
            ddlSeason.Items.Add("Cao điểm");
            ddlSeason.Items.Add("Cực cao điểm");

            ddlSeason.Items.Insert(0, new ListItem("---Chọn mùa du lịch---", "-1"));
        }
        private void LoadGroupType()
        {
            var ds = new DataSet();
            ds.ReadXml(MapPath("~/xml/GroupType.xml"));
            var dv = ds.Tables["GroupType"].DefaultView;
            ddlGroupType.DataTextField = "Name";
            ddlGroupType.DataValueField = "ID";
            ddlGroupType.DataSource = dv;
            ddlGroupType.DataBind();
            ddlGroupType.Items.Insert(0, new ListItem("--- Chọn loại hình đoàn ---", "-1"));
        }
        private void LoadNational()
        {
            ddlCusNationalId.DataSource = new NationalController().ListNational();
            ddlCusNationalId.DataTextField = "NationalName";
            ddlCusNationalId.DataValueField = "NationalId";
            ddlCusNationalId.DataBind();

            ddlCusNationalId.Items.Insert(0, new ListItem("--- Chọn quốc gia ---", "0"));
        }
        public Boolean GvPhanTrang(DataTable tb, ref GridView gv, int SoTrang, int TrangHienTai)
        {
            try
            {

                gv.DataSource = tb;
                gv.AllowPaging = true;
                gv.PageIndex = TrangHienTai;
                gv.PageSize = SoTrang;
                gv.DataBind();
                return true;
            }
            catch
            {
                return false;
            }
        }
        protected void ChangedGg2(object sender, EventArgs e)
        {
            LoadGvKhachHang(0);
        }
        protected void bntSaveUpd_Click(object sender, EventArgs e)
        {
            CustomersInfo info = new CustomersController().GetCustomers(Convert.ToInt32(txtCusId1.Text));
            info.CusFullName = txtCusFullName.Text;
            info.CusCode = txtCusCode.Text;
            info.CusPhone = txtCusPhone.Text;
            info.CusEmail = txtCusEmail.Text;
            info.CusDateofBirth = txtCusDateOfBirth.Text;
            info.CusAddress = txtCusAddress.Text;
            info.CusGender = Convert.ToInt32(RadioButtonList1.SelectedValue);
            info.CusNationalId = Convert.ToInt32(ddlCusNationalId.SelectedValue);
            info.CusGender = Convert.ToInt32(RadioButtonList1.SelectedValue);
            info.CusNotes = txtCusNotes.Text;
            info.CusUpdatedBy = UserId;
            info.CusUpdatedDate = DateTime.Now;
            info.CusStatus = 0;
            new CustomersController().UpdateCustomers(info);
            Response.Redirect("CusRequest.aspx");
        }
        protected void bntSaveIns_Click(object sender, EventArgs e)
        {
            CustomersInfo info = new CustomersInfo();
            info.CusFullName = txtCusFullName.Text;
            info.CusCode = txtCusCode.Text;
            info.CusPhone = txtCusPhone.Text;
            info.CusEmail = txtCusEmail.Text;
            info.CusAddress = txtCusAddress.Text;
            info.CusDateofBirth = txtCusDateOfBirth.Text;
            info.CusGender = Convert.ToInt32(RadioButtonList1.SelectedValue);
            info.CusNationalId = Convert.ToInt32(ddlCusNationalId.SelectedValue);
            info.CusGender = Convert.ToInt32(RadioButtonList1.SelectedValue);
            info.CusNotes = txtCusNotes.Text;
            info.CusCreatedBy = UserId;
            info.CusCreatedDate = DateTime.Now;
            info.CusStatus = 0;
            new CustomersController().InsertCustomers(info);
            Response.Redirect("CusRequest.aspx");
      
        }
        protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Label txtMa = (Label)(GridView2.Rows[e.RowIndex].FindControl("lbMa"));
            new CustomersController().DeleteCustomers(Convert.ToInt32(txtMa.Text));
            LoadGvKhachHang(0);
        }
        protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                foreach (Button button in e.Row.Cells[13].Controls.OfType<Button>())
                {
                    if (button.CommandName == "Delete")
                    {
                        button.Attributes["onclick"] = "if(!confirm('Do you want to delete  ?')){ return false; };";
                    }
                }
            }
        }
        #endregion


        #region
        public void LoadGv(int indexPage)
        {
            TourInfo tourInfor = new TourInfo();
            tourInfor.MaTour = txtMaTours.Text;
            tourInfor.TourName = txtTourName.Text;
            tourInfor.TourTypeId = Convert.ToInt32(ddlTourType.SelectedValue);
            tourInfor.GroupType = ddlGroupType.SelectedValue;
            tourInfor.Schedule = txtSchedule.Text;
            tourInfor.MartName = ddlMartName.SelectedValue;
            //tourInfor.HasLandTour=,
            //tourInfor.CTBeginDate = Convert.ToDateTime(txtBeginDate.Text);
            tourInfor.CTSeasion = ddlSeason.SelectedValue;
            tourInfor.QuantityDay = Convert.ToInt32(txtQuantityDay.Text);
            List<TourInfo> list = new TourController().ListTourSearch(tourInfor, 0);
            DataTable tb = CBO.ConvertToDataTable(list, typeof(TourInfo));
            GvPhanTrang(tb, ref GridView1, 20, indexPage);
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            LoadGvKhachHang(e.NewPageIndex);
        }
        protected void txtBeginDate_TextChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void txtMaTours_TextChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void txtTourName_TextChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void ddlTourType_TextChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void ddlGroupType_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void ddlMartName_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void txtQuantityDay_TextChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void txtSchedule_TextChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void ddlLandTour_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        protected void ddlSeason_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGv(0);
        }
        #endregion




    }
}